package cn.com.sinux.spring.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;

import javax.persistence.EntityManager;
import java.io.Serializable;

/**
 * 资源工厂
 * <p/>
 * Created:14-7-3 上午11:08
 *
 * @author <a href="mailto:caolei@sinosoft.co"> caolei </a>
 * @version 1.0
 */
public class BaseRepositoryFactoryBean<R extends JpaRepository<T, I>, T, I extends Serializable>
        extends JpaRepositoryFactoryBean<R, T, I>
{
    @SuppressWarnings("rawtypes")
	protected RepositoryFactorySupport createRepositoryFactory(EntityManager entityManager)
    {

        return new BaseRepositoryFactory(entityManager);
    }

    private static class BaseRepositoryFactory<T, I extends Serializable> extends JpaRepositoryFactory
    {

        private EntityManager entityManager;

        public BaseRepositoryFactory(EntityManager entityManager)
        {
            super(entityManager);
            this.entityManager = entityManager;
        }

//        protected Object getTargetRepository(RepositoryInformation information) {
//            return new BaseRepository<T, I>((Class<T>)information.getDomainType(), entityManager);
//        }
//
        protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata)
        {
            return IBaseRepository.class;
        }
    }
}
